package com.example.speeddating.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.Follow;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@RestController
public class FollowController {

    @Autowired
    private FollowService followService;

    /**
     * 关注用户
     * @param tokenEntity
     * @param jsonString
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/follow")
    @TokenCheck(roles = "user")
    public BaseResponse followUser(@LoginUser TokenEntity tokenEntity,@RequestBody HashMap<String,Object> jsonString){
        if(jsonString.get("followId")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        return RespGenerator.returnOK(followService.followUser(tokenEntity.getId(),Long.valueOf(jsonString.get("followId").toString())));
    }


    /**
     * 取关
     * @param tokenEntity
     * @param jsonString
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("/user/unfollow")
    @TokenCheck(roles = "user")
    public BaseResponse cancleFollowUser(@LoginUser TokenEntity tokenEntity,@RequestBody HashMap<String,Object> jsonString){
        if(jsonString.get("followId")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",tokenEntity.getId());
        queryWrapper.eq("follow_id",Long.valueOf(jsonString.get("followId").toString()));
        boolean save = followService.remove(queryWrapper);
        return RespGenerator.returnOK(save);
    }

    @PostMapping("/user/update/unreadFollow")
    @TokenCheck(roles = "user")
    public BaseResponse updateFollowUser(@LoginUser TokenEntity tokenEntity){
        followService.updateUnreadFollow(tokenEntity.getId());
        return RespGenerator.returnOK("更新成功");
    }


}
